package yunplus.io.hchome.presenter.implement;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

import yunplus.io.hchome.display.view.ILoginView;
import yunplus.io.hchome.model.ApiCode;
import yunplus.io.hchome.model.api.FPM;
import yunplus.io.hchome.model.api.contract.IFPMCallback;
import yunplus.io.hchome.model.storage.DomainShared;
import yunplus.io.hchome.model.storage.LoginShared;
import yunplus.io.hchome.presenter.contract.ILaunchPresenter;
import yunplus.io.hchome.presenter.contract.ILoginPresenter;
import yunplus.io.hchome.utils.PasswordEncoder;
import yunplus.io.hchome.utils.codec.AESUtil;

/**
 * Created by WangFan on 2016/12/29.
 */

public class LoginPresenter implements ILoginPresenter {

    private final static String TAG = "LoginPresenter";

    public final static String PORT = "10000";

    private Activity _mActivity;

    private ILoginView _mView;

    private JSONObject _mUser;

    private String _domain;

    private boolean _isLan = false;

    public LoginPresenter(Activity activity, ILoginView view) {
        this._mActivity = activity;
        this._mView = view;
    }

    @Override
    public void open() {
    }

    /**
     * fpm 初始化成功之后进行登陆操作
     * @param username
     * @param password
     */
    public void login(final String username, final String password){
        final JSONObject args = new JSONObject();
        args.put("username", username);
        args.put("password", PasswordEncoder.encodePass(password));
        Log.d(TAG, args.toJSONString());
        FPM.me().call(ApiCode.LOGIN, username, args, new IFPMCallback() {

            @Override
            public void onFailure(String code, String message) {
                _mView.onLoginError(message);
            }

            @Override
            public void onSuccess(JSONObject result) {
                //Save the user
                _mUser = new JSONObject();
                _mUser.put(LoginShared.KEY_ID, username);
                _mUser.put(LoginShared.KEY_LOGIN_NAME, username);
                _mUser.put(LoginShared.KEY_LOGIN_PASS, password);
                _mUser.put(LoginShared.KEY_DOMAIN, _domain);
                LoginShared.login(_mActivity, args.getString("password"), _mUser);
                //Save the domain
                DomainShared.saveDomain(_mActivity, _domain);
                DomainShared.setGatewayType(_mActivity, _isLan?"lan":"wlan");
                _mView.onLoginOkFromLan(_isLan);
            }
        });
    }

    @Override
    public void login(final String domain, final String username, final String password) {
        _mView.onLoginStart();
        _isLan = false;
        final String cgiUrl = "http://" + domain + "/cgi-bin/process.cgi";
        _domain = domain;
        FPM.me().init(cgiUrl);
        Log.d(TAG, cgiUrl);
        FPM.me().ping(new IFPMCallback() {
            @Override
            public void onFailure(String code, String message) {
                _mView.onLoginError(message);
            }

            @Override
            public void onSuccess(JSONObject result) {
                login(username, password);
            }
        });
    }

    @Override
    public void login(final String domain, final String username, final String password, final boolean isLan) {
        _isLan = isLan;
        if(!isLan){
            // wlan 外网
            this.login(domain, username, password);
        }else{
            _mView.onLoginStart();
            _domain = domain;
            FPM.me().init(ILaunchPresenter.LAN_CGI_URL);
            FPM.me().ping(new IFPMCallback() {
                @Override
                public void onFailure(String code, String message) {
                    _mView.onLoginError(message);
                }

                @Override
                public void onSuccess(JSONObject result) {
                    login(username, password);
                }
            });
        }

    }
}
